package controller.frontOffice;

import java.io.Serializable;
import java.util.Properties;

import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import controller.backOffice.ClientController;

import entities.Client;

@Named
@SessionScoped
//@ManagedBean
//@ViewScoped
public class EmailController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Client client = new Client(); 
	
	private String contenu;

	public String contact()
	{
		
		
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		javax.mail.Session session = javax.mail.Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("j2ee.srong@gmail.com","cscscscs");
			}
		});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("j2ee.srong@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse("j2ee.srong@gmail.com"));
			message.setSubject("Email de test");
			message.setText("Vous avez recu un message de : " + client.getNom()+ " "+ client.getPrenom() + " dont le mail est : " + client.getEmail()+ "."  
					+ "\n" + "Contenu du message : " + "\n" + getContenu());

			Transport.send(message);

			System.out.println("email ok");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}

		return "home";	
	}

	public String getContenu() {
		return contenu;
	}

	public void setContenu(String contenu) {
		this.contenu = contenu;
	}

	public Client getClient() {
		if (client == null ) {
			client = new Client();
		}
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}	
	
	
}
